home *** CD-ROM | disk | FTP | other *** search
/ 5 Star Games: DOS Edition 2 / 5 Star Games - DOS Edition (1995)(Ready to Run).iso / dbc / db_pswd.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  228 lines

  1. /****************************************************************************/
  2. /*                         DATABOSS MODULE: DB_PSWD.C                       */
  3. /****************************************************************************/
  4.  
  5. #include "db_lsc.h"
  6.  
  7. #ifndef __TURBOC__
  8.     #include <graph.h>
  9.     #include <sys\types.h>
  10.     #define _close(h) close(h)
  11. #endif
  12. #include <io.h>
  13. #include <fcntl.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <sys\stat.h>
  17. #include "db_types.h"
  18. #include "db_curs.h"
  19. #include "db_file.h"
  20. #include "db_funcs.h"
  21. #include "db_gvar.h"
  22. #include "db_key.h"
  23. #include "db_str.h"
  24. #include "db_win.h"
  25. #include "db_util.h"
  26. #include "db_pswd.h"
  27.  
  28. /*****************************  INTERNAL TYPES  *****************************/
  29.  
  30. typedef struct {
  31.     uchar exeid[9];
  32.     uchar password[9];
  33. } default_rec;
  34.  
  35. /***************************  INTERNAL CONSTANTS  ***************************/
  36.  
  37. static uchar TwistMask[9] = "ôö╘f8d¢º";
  38. static default_rec def = {"öDBPSWD_","\xB9\x15\xE1\xB3\xC7\xED\xA9\x8C"};
  39.  
  40. /***************************  INTERNAL VARIABLES  ***************************/
  41.  
  42. static byte longest;
  43. static int pwdfile;
  44. static bool pwdopen;
  45. static uchar opname[9],pword[9];
  46.  
  47. static bool initialized = False;
  48.  
  49. /*****************************  IMPLEMENTATION  *****************************/
  50.  
  51. strptr twist(string sout, byte mode, string instr, byte s)        /* SN 3.5 */
  52. {
  53.     int b,c,ilen;
  54.     string ts;
  55.  
  56.     ilen = strlen(instr);
  57.     for (b = 0 ; b < ilen ; b++) {
  58.         c = (ilen - (b+1) + 3) % ilen;
  59.         if (mode > 0)
  60.             ts[b] = instr[c] + TwistMask[(c+1) % 8] + (9 - s);       /* SN 3.5 */
  61.         else
  62.             ts[b] = instr[c] - TwistMask[(b+1) % 8] + (9 - s);       /* SN 3.5 */
  63.     }
  64.     ts[ilen] = '\0';
  65.     return (strcpy(sout,ts));
  66. }
  67.  
  68. void convertrec(passwd_rec *pwrec)
  69. {
  70.     if (pwrec->operator[0] < ' ') {  /* Pascal data record if length byte detected */
  71.         memmove(&pwrec->operator[0],&pwrec->operator[1],8);
  72.         pwrec->operator[8] = '\0';
  73.         memmove(&pwrec->password[0],&pwrec->password[1],8);
  74.         pwrec->password[8] = '\0';
  75.         memmove(&pwrec->sec_code[0],&pwrec->sec_code[1],1);
  76.         pwrec->sec_code[1] = '\0';
  77.     }
  78. }
  79.  
  80. bool pswdfound(void)
  81. {
  82.     int i;
  83.     uchar ch, compstr[9];
  84.     passwd_rec passwd;
  85.     bool pswdmatch;
  86.  
  87.     upper(pword,pword);
  88.     upper(compstr,twist(compstr,0,def.password,9));                   /* SN 3.5 */
  89.     if ((strcmp(opname,"SYSOP   ") == 0) && (strcmp(pword,compstr) == 0)) {
  90.         strcpy(gvar->operator,"SYSOP   ");
  91.         gvar->sec = 9;
  92.         pswdmatch = True;
  93.     }
  94.     else {
  95.         pswdmatch = False;
  96.         if (pwdopen) {
  97.             if (filelength(pwdfile) > sizeof(passwd_rec)) {
  98.                 i = 1;
  99.                 ch = ' ';
  100.                 do {
  101.                     lseek(pwdfile,(long) (i*sizeof(passwd_rec)),SEEK_SET);
  102.                     read(pwdfile,(void *)&passwd,sizeof(passwd_rec));
  103.                     convertrec(&passwd);
  104.                     upper(compstr,twist(compstr,0,passwd.password,(byte) ival(passwd.sec_code)));   /* SN 3.5 */
  105.                     i++;
  106.                     if (kpressed()) ch = getkey();
  107.                     pswdmatch = (bool) ((strcmp(pword,compstr) == 0) &&
  108.                                                             (strcmp(opname,passwd.operator) == 0) &&
  109.                                                             (passwd.recstat == 0));
  110.                 } while ((ch != QitKey) && (!pswdmatch) &&
  111.                                     (i < (int)(filelength(pwdfile)/sizeof(passwd_rec))));
  112.             }
  113.             if (pswdmatch) {
  114.                 gvar->sec = (byte) ival(passwd.sec_code);
  115.                 strcpy(gvar->operator,passwd.operator);
  116.             }
  117.         }
  118.     }
  119.     return (pswdmatch);
  120. }
  121.  
  122. void logon(void)
  123. {
  124.     bool tb;
  125.     winptr twinp;
  126.     pathstr fname;
  127.     uchar tpic[3];
  128.  
  129.     strcpy(tpic, "8X");
  130.     gvar->sec = 0;
  131.     if (openwin(0,18,10,longest,1,78,78,8,"─┐│┘─└│┌",TopCnt,LSC_LogonOrEsc)) {     /* SN 3.5 */
  132.         twinp = curwin;
  133.         twinp->disp = True;
  134.         dispwindow(twinp);
  135.         strip(fname,strconcat(fname,thisprog.fdir,gvar->fname,".PWD",NULL));
  136.         pwdopen = (bool) ((pwdfile = open(fname,O_BINARY | O_RDWR, S_IREAD | S_IWRITE)) != -1);
  137.         writewxy(LSC_OperatorPr,0,1,1,twinp);                                       /* SN 3.5 */
  138.         strcpy(opname,"        ");
  139.         tpic[1] = Pic_U;        /* operator name entry in upper case */
  140.         edt_fld(strlen(LSC_OperatorPr)+1,1,opname,_Ch,tpic,0,78,twinp);             /* SN 3.5 */
  141.         writewxy(LSC_PasswordPr,0,1,1,twinp);                                       /* SN 3.5 */
  142.         tpic[1] = Pic_B;  /* no echo of following password */
  143.         do {
  144.             strcpy(pword,"        ");
  145.             edt_fld(strlen(LSC_PasswordPr)+1,1,pword,_Ch,tpic,0,78,twinp);           /* SN 3.5 */
  146.             tb = pswdfound();
  147.             if (!tb) noise(900,0.1);
  148.         } while ((exitcode != QitKey) && !(tb && ((exitcode == XeptKey) || (exitcode == Enter))));
  149.         if (pwdopen) _close(pwdfile);
  150.         closewin(&twinp);
  151.     }
  152. }
  153.  
  154. strptr updpword(string sout, string oldpw, byte s)
  155. {
  156.     winptr twinp;
  157.     uchar pw1[9],pw2[9];
  158.     uchar tpic[3];
  159.  
  160.     strcpy(tpic, "8X");
  161.     sout[0] = '\0';
  162.     if (openwin(0,18,10,longest,1,78,78,8,"─┐│┘─└│┌",TopCnt,LSC_ChangePasswordPr)) {     /* SN 3.5 */
  163.         twinp = curwin;
  164.         twinp->disp = True;
  165.         dispwindow(twinp);
  166.         tpic[1] = Pic_B;  /* no echo of password */
  167.         do {
  168.             strcpy(pw1,"        ");
  169.             strcpy(pw2,pw1);
  170.             writewxy(LSC_PasswordPr,0,1,1,twinp);                                          /* SN 3.5 */
  171.             edt_fld(strlen(LSC_PasswordPr)+1,1,pw1,_Ch,tpic,0,78,twinp);                   /* SN 3.5 */
  172.             upper(pw1,pw1);
  173.             if (exitcode != QitKey) {
  174.                 writewxy(LSC_VerifyPr,0,1,1,twinp);                                         /* SN 3.5 */
  175.                 edt_fld(strlen(LSC_PasswordPr)+1,1,pw2,_Ch,tpic,0,78,twinp);                /* SN 3.5 */
  176.                 upper(pw2,pw2);
  177.                 if ((strcmp(pw1,"        ") == 0) || (strcmp(pw1,pw2) != 0)) noise(2000,0.1);
  178.             }
  179.         } while ((exitcode != QitKey) && !((strcmp(pw1,"        ") != 0) && (strcmp(pw1,pw2) == 0)));
  180.         twist(pw1,1,pw1,s);                                                               /* SN 3.5 */
  181.  
  182.         if ((exitcode != QitKey) && (strcmp(pw1,oldpw) != 0)) strcpy(sout,pw1);
  183.         closewin(&twinp);
  184.     }
  185.     return(sout);
  186. }
  187.  
  188. void updmaster(void)
  189. {
  190.     winptr twinp;
  191.     uchar pw1[9],pw2[9];
  192.  
  193.     updpword(pw1,def.password,9);                                                        /* SN 3.5 */
  194.     if (pw1[0] != '\0') {
  195.         strcpy(def.password,pw1);
  196.         message(NULL,LSC_UpdatingMaster);                                                 /* SN 3.5 */
  197.         updexe(def.exeid,sizeof(def));
  198.         message(NULL,"");
  199.     }
  200. }
  201.  
  202. void setlongest(void)
  203. {
  204.     longest = maximum(strlen(LSC_LogonOrEsc), strlen(LSC_PasswordPr));                    /* SN 3.5 */
  205.    longest = maximum(longest, strlen(LSC_ChangePasswordPr));                            /* SN 3.5 */
  206.    longest = maximum(longest, strlen(LSC_VerifyPr));                                    /* SN 3.5 */
  207.    longest = maximum(longest, strlen(LSC_UpdatingMaster));                              /* SN 3.5 */
  208.    longest = maximum(longest, strlen(LSC_OperatorPr));                                  /* SN 3.5 */
  209.     longest += 12;                                                                       /* SN 3.5 */
  210. }
  211.  
  212. /**********************  UNIT INITIALIZATION/EXIT CODE  *********************/
  213.  
  214. void db_pswd_init(void)
  215. {
  216.     if (!initialized) {
  217.         initialized = True;
  218.         db_funcs_init();
  219.         db_gvar_init();
  220.         db_key_init();
  221.         db_util_init();
  222.         db_win_init();
  223.         setlongest();
  224.     }
  225. }
  226.  
  227. /*****************************  END OF DB_PSWD.C  ***************************/
  228.